package com.bc.bcbuyerapihttp.controller;

import com.bc.bcapi.service.*;
import com.bc.bcbuyerapihttp.common.UserContext;
import com.bc.bcmodel.BuyerUserInfo;
import com.bc.bcmodel.domain.T_User_buyer_infoModel;
import com.bc.bcmodel.req.AccountDryWithDrawReq;
import com.bc.bcmodel.req.AccountWithdrawReq;
import com.bc.bcmodel.req.CommReq;
import com.bc.commoncore.util.Digest;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @Name WalletContrller
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/9/8 11:07
 * @Version 1.0
 **/

@RestController
@RequestMapping("wallet")
@Api(value = "买家钱包接口")
public class WalletContrller {

    @Autowired
    T_Wallet_infoService t_wallet_infoService;
    @Autowired
    T_User_walletService t_user_walletService;
    @Autowired
    T_User_mytokenService t_user_mytokenService;
    @Autowired
    T_User_google_authService t_user_google_authService;
    @Autowired
    T_Account_logService t_account_logService;
    @Autowired
    T_User_withdrawService t_user_withdrawService;
    @Autowired
    T_User_buyer_infoService t_user_buyer_infoService;

    @ApiOperation("准备提币")
    @RequestMapping(value = "drywithdraw", method = RequestMethod.POST)
    public JsonResult drywithdraw(@RequestBody AccountDryWithDrawReq req) throws Exception {
        BuyerUserInfo user_info = UserContext.getCurrentContext().getLoginInfo();
        if (user_info == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        return t_wallet_infoService.dryWithdraw(req, user_info.getUser_id());
    }

    @ApiOperation("提币")
    @RequestMapping(value = "withdraw", method = RequestMethod.POST)
    public JsonResult withdraw(HttpServletRequest request, @RequestBody AccountWithdrawReq req) throws Exception {
        BuyerUserInfo user_info = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (user_info == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        String user_id = user_info.getUser_id();
        /**
         * 验证交易密码开始
         * */
        T_User_buyer_infoModel t_user_buyer_infoModel = new T_User_buyer_infoModel();
        t_user_buyer_infoModel.setUser_id(user_id);
        T_User_buyer_infoModel userInfoResult = t_user_buyer_infoService.findByNamedParam(t_user_buyer_infoModel);
        if (userInfoResult == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        String password = Digest.signMD5(req.getPassword(), Digest.ENCODE);
        if (!password.equals(userInfoResult.getPassword())) {
            return new JsonResult(HttpReturnRnums.USER_ACCOUNT_PWD_ERROR);
        }
        /**
         * 验证交易密码结束
         * */
        String authtoken = req.getAuthtoken();
        boolean googleauthverify = t_user_google_authService.verify(authtoken, user_id);
        if (!googleauthverify) {
            return new JsonResult(HttpReturnRnums.GOOGLE_AUTH_AUTHORIZATION_FAILED);
        }

        req.setPlatform(2);
        return t_user_walletService.withdraw(req, user_id, request);
    }

    @ApiOperation("获取钱包地址")
    @RequestMapping(value = "show", method = RequestMethod.GET)
    public JsonResult show() {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }

        return t_user_walletService.getWalletAddress(loginInfo.getUser_id());
    }

    @ApiOperation("token列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public JsonResult list() {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        return t_user_mytokenService.getUserAllToken(loginInfo.getUser_id());
    }

    @ApiOperation("交易流水")
    @RequestMapping(value = "translist", method = RequestMethod.POST)
    public JsonResult translist(HttpServletRequest request, @RequestBody CommReq req) {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }

        return t_account_logService.getMyAccountInfo(request, req, loginInfo.getUser_id());
    }

    @ApiOperation("外部交易流水")
    @RequestMapping(value = "outtranslist", method = RequestMethod.POST)
    public JsonResult outtranslist(HttpServletRequest request, @RequestBody CommReq req) {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }

        return t_account_logService.getMyAccountOutTransInfo(request, req, loginInfo.getUser_id());
    }

    @ApiOperation("提币历史")
    @RequestMapping(value = "withdrawhistory", method = RequestMethod.POST)
    public JsonResult withdrawhistory(HttpServletRequest request, @RequestBody CommReq req) {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        return t_user_withdrawService.withdrawHistory(request, req, loginInfo.getUser_id());
    }

}
